<?php

/**
 * param.class.php 参数处理类
 * @Author: Administrator
 * @Date:   2018-11-28 14:53:33
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-11-28 16:01:40
 */
class param
{
	//路由配置
	private $route_config = '';

	/**
	 * [构造方法]
	 */
	public function __construct(){
		if(!get_magic_quotes_gpc()) {
			$_POST = new_addslashes($_POST);
			$_GET = new_addslashes($_GET);
			$_REQUEST = new_addslashes($_REQUEST);
			$_COOKIE = new_addslashes($_COOKIE);
		}

		$this->route_config = base::load_config('route', SITE_URL) ? base::load_config('route', SITE_URL) : base::load_config('route', 'default');

		if(isset($this->route_config['data']['POST']) && is_array($this->route_config['data']['POST'])) {
			foreach($this->route_config['data']['POST'] as $_key => $_value) {
				if(!isset($_POST[$_key])) $_POST[$_key] = $_value;
			}
		}

		if(isset($this->route_config['data']['GET']) && is_array($this->route_config['data']['GET'])) {
			foreach($this->route_config['data']['GET'] as $_key => $_value) {
				if(!isset($_GET[$_key])) $_GET[$_key] = $_value;
			}
		}

		if(isset($_GET['page'])) {
			$_GET['page'] = max(intval($_GET['page']),1);
			$_GET['page'] = min($_GET['page'],1000000000);
		}

		return true;
	}

	/**
	 * [获取模型]
	 * @return [type] [description]
	 */
	public function route_m(){
		$m = isset($_GET['m']) && !empty($_GET['m']) ? $_GET['m'] : (isset($_POST['m']) && !empty($_POST['m']) ? $_POST['m'] : '');
		$m = $this->safe_deal($m);
		if (empty($m)) {
			return $this->route_config['m'];
		} else {
			if(is_string($m)) return $m;
		}
	}

	/**
	 * [获取控制器]
	 * @return [type] [description]
	 */
	public function route_c(){
		$c = isset($_GET['c']) && !empty($_GET['c']) ? $_GET['c'] : (isset($_POST['c']) && !empty($_POST['c']) ? $_POST['c'] : '');
		$c = $this->safe_deal($c);
		if (empty($c)) {
			return $this->route_config['c'];
		} else {
			if(is_string($c)) return $c;
		}
	}

	/**
	 * [获取方法]
	 * @return [type] [description]
	 */
	public function route_a(){
		$a = isset($_GET['a']) && !empty($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) && !empty($_POST['a']) ? $_POST['a'] : '');
		$a = $this->safe_deal($a);
		if (empty($a)) {
			return $this->route_config['a'];
		} else {
			if(is_string($a)) return $a;
		}
	}

	/**
	 * [安全处理函数]
	 * @param  [type] $str [description]
	 * @return [type]      [description]
	 */
	private function safe_deal($str) {
		return str_replace(array('/', '.'), '', $str);
	}
}